package logic

import (
	"context"
	"errors"
	"practice-one/models/mysql"

	"practice-one/userrpc/internal/svc"
	"practice-one/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *userrpc.CollectAddRequest) (*userrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}
	var goods mysql.Goods
	if err := goods.FindGoodsById(mysql.DB, in.GoodsId); err != nil {
		return nil, errors.New("商品不存在")
	}
	collect := &mysql.Collect{
		UserId:  in.UserId,
		GoodsId: in.GoodsId,
	}
	if err := collect.CollectAdd(mysql.DB); err != nil {
		return nil, err
	}
	return &userrpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
